/**
 * Created by jun.zhang on 14-8-18.
 */

var game = new Phaser.Game(320, 480, Phaser.CANVAS, 'phaser-box');
var bingzi = {"result":0};
var bz_round = 1; // 回合数
var bz_action = 0; // 出拳动作

game.States = {};
game.States.boot = function()
{
    this.preload = function()
    {
        game.load.image('loading','img/preloader.gif');
    };

    this.create = function()
    {
        game.state.start('preload');
    };
};

game.States.preload = function()
{
    this.preload = function()
    {
        var loading = game.add.sprite( game.width/2-110, game.height/2-10, 'loading');
        game.load.setPreloadSprite(loading);

        game.load.image('background','img/bg.png');
        game.load.image('title','img/title.png');
        game.load.image('copyright','img/copyright.png');

        game.load.image('icon_help','img/icon_help.png');

        game.load.image('btn_start','img/btn_start.png');
        game.load.image('btn_share','img/btn_share.png');
        game.load.image('btn_continue','img/btn_continue.png');

        game.load.image('scissors','img/scissors.png');
        game.load.image('stone','img/stone.png');
        game.load.image('cloth','img/cloth.png');
        game.load.image('bomb','img/bomb.png');
    };

    this.create = function()
    {
        game.state.start('menu');
    };
};

bingzi.platform = function(game)
{
    game.add.tileSprite(0, 0, game.width, game.height,'background');

    var btn_copyright = game.add.button(game.width/2, game.height-20,'copyright', bz_help);
    btn_copyright.anchor.setTo(0.5,0.5);
};

game.States.menu = function()
{
    this.create = function()
    {
        bingzi.platform(game);

        var icon_help = game.add.button(10, 10,'icon_help', bz_help);

        var title = game.add.group();
        title.create(0,0,'title');
        title.x = 35;
        title.y = 100;
        game.add.tween(title).to({ y:120 },1000,null,true,0,Number.MAX_VALUE,true);

        var btn_start = game.add.button(game.width/2, game.height/2+60, 'btn_start', function(){
            game.state.start('play');
        });
        btn_start.anchor.setTo(0.5,0.5);
    };
};

game.States.play = function()
{
    this.create = function()
    {
        bingzi.platform(game);

        var round_txt = game.add.text(game.width/2, game.height/2-110,'Round '+bz_round);
        round_txt.anchor.setTo(0.5,0.5);

        bz_action = 0;

        var scissors = game.add.button(game.width/2, game.height/2, 'scissors', function(){
            bz_action = 1;
            game.state.start('round');
        });
        scissors.anchor.setTo(0.5,0.5);
        var stone = game.add.button(game.width/2-90, game.height/2+110, 'stone', function(){
            bz_action = 2;
            game.state.start('round');
        });
        stone.anchor.setTo(0.5,0.5);
        var cloth = game.add.button(game.width/2+90, game.height/2+110, 'cloth', function(){
            bz_action = 3;
            game.state.start('round');
        });
        cloth.anchor.setTo(0.5,0.5);
    };
};

bingzi.get_random = function(min,max)
{
    var range = max - min;
    var rand = Math.random();
    return(min + Math.round(rand * range));
};

bingzi.action = function(action)
{
    var res = '';
    switch(action)
    {
        case 1:
            res = 'scissors';
            break;
        case 2:
            res = 'stone';
            break;
        case 3:
            res = 'cloth';
            break;
    }
    return res;
};

bingzi.round = function(game, man, computer)
{
    var bomb_img = game.add.image(game.width/2, game.height/2+60, 'bomb');
    bomb_img.anchor.setTo(0.5, 0.5);

    var cpu_action = bingzi.action(computer);
    var cpu_img = game.add.image(game.width/2, game.height/2, cpu_action);
    cpu_img.anchor.setTo(0.5, 0.5);

    var man_action = bingzi.action(man);
    var man_img = game.add.image(game.width/2, game.height/2+110, man_action);
    man_img.anchor.setTo(0.5, 0.5);

    // 输:-1/平:0/赢:1
    return (man - computer + 4)%3 - 1;
};

game.States.round = function()
{
    this.create = function()
    {
        bingzi.platform(game);

        var timeout = 1000;
        var bz_android = bingzi.get_random(1,3);

        // 第一轮玩家赢，并延长到两倍显示时间
        if( bz_round == 1 )
        {
            bz_android = (bz_action==1) ? 3:bz_action-1;
            timeout += 1000;
        }

        // 第二轮玩家平，并延长到1.5倍显示时间
        if(  bz_round == 2 )
        {
            bz_android = bz_action;
            timeout += 500;
        }

        bingzi.result = bingzi.round(game, bz_action, bz_android);

        var result_res = (bingzi.result>=0) ? 'Win' : 'Lose';
        var result_txt = game.add.text(game.width/2, game.height/2-110, result_res+' ! ! !');
        result_txt.anchor.setTo(0.5,0.5);

        var replay = function()
        {
            // 修改分享title
            shareTitle = '我连胜 '+bz_round+' 局，你敢来挑战么？';

            if( bingzi.result >= 0 )
            {
                bz_round++;
                game.state.start('play');
            }
            else
            {
                game.state.start('result');
            }
        };
        setTimeout(replay, timeout);
    };
};

game.States.result = function()
{
    this.create = function()
    {
        bingzi.platform(game);

        var oh_yeah_txt = game.add.text(game.width/2, game.height/2-110, 'Oh Yeah !');
        oh_yeah_txt.anchor.setTo(0.5,0.5);

        var result_txt = game.add.text(game.width/2, game.height/2-30, '连赢 '+bz_round+' 局!');
        result_txt.anchor.setTo(0.5,0.5);

        var btn_share = game.add.button(game.width/2, game.height/2+50, 'btn_share', weixin_share);
        btn_share.anchor.setTo(0.5,0.5);

        var btn_continue = game.add.button(game.width/2, game.height/2+110, 'btn_continue', function(){
            bz_round = 1;
            game.state.start('play');
        });
        btn_continue.anchor.setTo(0.5,0.5);
    };
};

game.state.add('boot',game.States.boot);
game.state.add('preload',game.States.preload);
game.state.add('menu',game.States.menu);
game.state.add('play',game.States.play);
game.state.add('round',game.States.round);
game.state.add('result',game.States.result);

game.state.start('boot');